﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.Design;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using T.ApiResults;
using T.Basic.API.Appliction.Command.CargoOwnerCommand;
using T.Basic.Domain.T.Basic.Model;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;


namespace T.Basic.API.Controllers
{
    /// <summary>
    /// 货主信息控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class CargoOwnerController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        public CargoOwnerController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 添加货主信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreateCargoOwner(CargoOwnerCreateCommand command)
        {
            ApiResult<int> result=new ApiResult<int>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//公司ID
                command.User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);//用户ID
                command.CreatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//创建人
                result.Code = await _mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "添加货主信息失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 批量删除货主信息
        /// </summary>
        /// <param name="Command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteAllCargoOwner(CargoOwnerDeleteAllCommand Command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                Command.DeletedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//删除人
                Command.DeletedDate = DateTime.Now;//删除时间
                result.Code = await _mediator.Send(Command);

            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "批删货主信息失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除货主信息（单删）
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteCargoOwner([FromQuery]CargoOwnerDeleteCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {

                command.DeletedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//删除人
                command.DeletedDate = DateTime.Now;//删除时间
                result.Code = await _mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "删除（单删）货主信息失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填货主信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<CargoOwner>> GetCargoOwnerByAsync([FromQuery] CargoOwnerGetByIdCommand command)
        {
            ApiResult<CargoOwner> result = new ApiResult<CargoOwner>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

            //try
            //{
            //    return  await _mediator.Send(command);
            //}
            //catch (Exception)
            //{

            //    throw;
            //}
        }
        /// <summary>
        /// 修改货主信息
        /// </summary>
        /// <param name="Command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateCargoOwner(CargoOwnerUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//公司ID
                command.User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);//用户ID
                command.UpdatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//修改人
                command.UpdatedDate = DateTime.Now;//修改时间
                result.Code = await _mediator.Send(command);
               

            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "修改货主信息失败";
                throw;
            }
            return result;

        }

        /// <summary>
        /// 长传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public IActionResult GetPicName(IFormFile file) 
        {
            try 
            {
                string savePth=Directory.GetCurrentDirectory()+@"\wwwroot\Pic\";
                string NewFileName=Guid.NewGuid().ToString()+Path.GetExtension(file.FileName);
                using (FileStream fs = new FileStream(savePth + NewFileName, FileMode.Create))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }
                return Ok(NewFileName);
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}
